from django.template import Library
from django.shortcuts import render
from apps.web import models

register = Library()


@register.simple_tag
def use_space(used_space):
    """
    计算已使用空间，并把数据库字节转换为MB
    :param used_space: 项目的已使用空间
    :return:
    """
    # 大于GB
    if used_space >= 1024 ** 3:
        mb_value = used_space / (1024 ** 3)
        return f'{mb_value:.2f} GB'
    elif used_space >= 1024 ** 2:
        mb_value = used_space / 1024 / 1024
        return f'{mb_value:.2f} MB'
    elif used_space > 1024:
        mb_value = used_space / 1024
        return f'{mb_value:.2f} KB'
    elif used_space <= 1024:
        return f'{used_space} B'

